package cn.hs.service.impl;/**
 * @author Jovan Hou 2024/10/24
 **/

import cn.hs.entity.Charge;
import cn.hs.entity.Chargetype;
import cn.hs.entity.Consult;
import cn.hs.mapper.ChargeMapper;
import cn.hs.mapper.ChargetypeMapper;
import cn.hs.mapper.ConsultMapper;
import cn.hs.service.ChargeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @ClassName ChargeServiceImpl
 * @Description TODO
 * @Author CnYuu
 * @Date 2024年10月24日 10:46
 * @Version 1.0
 */
@Service
public class ChargeServiceImpl implements ChargeService
{
    @Autowired
    private ChargeMapper chargeMapper;
    @Autowired
    private ChargetypeMapper chargetypeMapper;
    @Autowired
    private ConsultMapper consultMapper;

    @Override
    public PageInfo findAll(Integer pageNum, Integer pageSize, Charge charge)
    {
        PageHelper.startPage(pageNum, pageSize);
        if (charge.getConsult()!=null){
            List<Integer> all = consultMapper.findIdsByName(charge.getConsult());
            charge.setConsultIds(all);
        }
        List<Charge> charges = chargeMapper.selectAll(charge);

        for (Charge charge1 : charges) {
            Integer consultId = charge1.getConsultId();
            Integer typeId = charge1.getTypeId();
            Consult consult = consultMapper.selectByPrimaryKey(Long.valueOf(consultId));
            Chargetype chargetype = chargetypeMapper.selectByPrimaryKey(Long.valueOf(typeId));
            charge1.setChargetype(chargetype);
            charge1.setConsult(consult);
        }
        PageInfo pageInfo = new PageInfo(charges);
        return pageInfo;
    }

    @Override
    public int insert(Charge charge)
    {
        return chargeMapper.insert(charge);
    }

    @Override
    public int update(Charge charge)
    {
        return chargeMapper.updateByPrimaryKey(charge);
    }

    @Override
    public int delete(Integer id)
    {
        return chargeMapper.deleteByPrimaryKey(Long.valueOf(id) );
    }

    @Override
    public List<Consult> selectAllConsult() {
        return chargeMapper.selectAllConsult();
    }

    @Override
    public List<Charge> selectByConsultId(Integer consultId) {
        return chargeMapper.selectByConsultId(consultId);
    }

}
